﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Reflection;
using System.Globalization;
using Microsoft.Office.Core;
using System.Windows.Forms;

namespace RemindMe
{
    public partial class ThisAddIn
    {
        internal Outlook.Application application;
        internal CommandBarButton toolbarButton;

        private void AddButton()
        {
            this.toolbarButton = (CommandBarButton) this.application.ActiveExplorer().CommandBars["Standard"].Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, Missing.Value, true);
            this.toolbarButton.Caption = "RemindMe";
            this.toolbarButton.Style = MsoButtonStyle.msoButtonCaption;
            this.toolbarButton.Tag = "RemindMe Settings";
            this.toolbarButton.Click += new _CommandBarButtonEvents_ClickEventHandler(toolbarButton_Click);
            this.toolbarButton.Visible = true;
        }

        DialogResult showSettings()
        {
            SettingsForm settingsForm = new SettingsForm(RemindMe.Properties.Settings.Default);
            return settingsForm.ShowDialog();
        }

        void toolbarButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
        {
            showSettings();
        }

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.application = new Outlook.Application();
            this.AddButton();

            if (RemindMe.Properties.Settings.Default.Email.Length < 1)
            {
                showSettings();
            }
            this.application.Reminder += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ReminderEventHandler(application_Reminder);
        }

        void application_Reminder(object Item)
        {
            Type itemType = Item.GetType();
            string subject = (string) itemType.GetProperty("Subject").GetValue(Item, null);
            string location = (string) itemType.GetProperty("Location").GetValue(Item, null);

            Outlook.MailItem email = (Outlook.MailItem)this.application.CreateItem(Outlook.OlItemType.olMailItem);
            email.To = RemindMe.Properties.Settings.Default.Email;
            email.Subject = String.Format(CultureInfo.CurrentCulture, "{0}{1}", RemindMe.Properties.Settings.Default.Subject, subject);
            email.Body = String.Format(CultureInfo.CurrentCulture, "{0}\n{1}", location, subject);
            (email as Outlook._MailItem).Send();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
